/**
 * ResourceTranslationServiceImpl.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.services;

import java.util.Properties;

import extjsdyntran.servlets.Constants;
import extjsdyntran.translation.ITranslationService;
import extjsdyntran.translation.ServiceTypes;
import extjsdyntran.translation.TranslationFactory;
import extjsdyntran.translation.TranslationServiceAbstract;

/**
 * @description Resource Translation Service 
 * Resource names are mapped to default strings. 
 * Default strings are translated by other translation services to the requested language translation.
 * This allows to set resource id's to components instead of real text values
 * 
 * @version 1.6
 * @status - development started, do not distribute yet
 * 
 * this could also extend custom service to enable grouping of resource strings
 */
public final class ResourceTranslationServiceImpl extends TranslationServiceAbstract {

	private static final long serialVersionUID = 1L;
	private static final String RESOURCE_KEY = "RESOURCES";
	private Properties customlang = null; 

	public ResourceTranslationServiceImpl(String username, String password,	String _path) {
		super(username, password, _path);
		customlang = new Properties();
		customlang.put(RESOURCE_KEY, "res");
		//this.translations.put(RESOURCE_KEY, new Properties());
		loadLanguages();
	}
	
	public boolean isNewLanguageSupported(){
		return false;
	}


	//should check for all services as it is only resource proxy to translation service
	//and holds only resource string id's with default values
	public boolean isLanguageSupported(String language){
		return customlang.containsKey(language);
	}
	
	@Override 
	public void loadLanguages() {
		super.loadLanguages(super.enumerationToStringArray(customlang));
	}
	

	@Override
	public boolean test() {
		return true;
	}

	//for now do nothing, for future version when multiple resource groups available 
	public boolean removeLanguageFromCache(String language){
		return false;
	}

	//for now do nothing, for future version when multiple resource groups available
	public boolean appendLanguageToCache(String language){
		return false;	
	}

	
	
	@Override
	//parameter should receive language , not RESOURCE
	//Use default value from incoming resource string id, then try to translate that value to appropriate requested language
	//could return resourceID, resource default value or translated value
	public String translate(String resID, String language) {
		String resStr = null;
		try {
			ITranslationService service = TranslationFactory.getService(language);

			//is it resource string
			Properties resList = (Properties) translations.get(RESOURCE_KEY);
			if(resList!=null)
				resStr = (String) resList.get(resID);
			
			//no, then try to translate by standard translation service
			if (resStr==null){
				return service.translate(resID, language);  
			}
			
			//yes, then translate resource string value to requested language					
			String res = service.translate(resStr, language);
			return res;
			//if res.equals(resStr) 

		} catch (Exception e) {
			logger.error("Error trying to translate resource string {} to language {} for value : {}", new String[]{resID, language,resStr});
			logger.error("",e);
			return Constants.json_false;
		}
	}


	@Override
	public Properties getListOfLanguages() {
		return customlang;
	}

	public ServiceTypes getServiceType(){
		return ServiceTypes.PROXY;
	}

	
}